package com.deng.common.view
{
	import com.deng.common.vo.HisOrganizationVo;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.events.FlexEvent;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class HisOrganizationCB extends CheckBox
	{
		private var pvo:HisOrganizationVo;
		
		public function HisOrganizationCB()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,upEvt);
			this.addEventListener(FlexEvent.SHOW,showHandler);
			this.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
			super();
		}
		private function keyHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == 32)
			{
				event.stopImmediatePropagation();
				return;
			}
		}
		private function upEvt(evt:MouseEvent):void{
			var ro:RemoteObject = new RemoteObject();
			if(pvo.type == "operate"){
				ro.destination = "OperateDepartmentImpl";			
				ro.addEventListener(FaultEvent.FAULT,flt);
				ro.addEventListener(ResultEvent.RESULT,updateRst);
				if(this.selected){//之前选中的，后面要删除
//					Alert.show("之前选中的，后面要删除");
					ro.deleteHisCode(pvo);
					(this.data as HisOrganizationVo).selected = 0;
					pvo.selected = 0;
				}else{
//					Alert.show("之前删除的，后面要选中");
					ro.addHisCode(pvo);
					(this.data as HisOrganizationVo).selected = 1;
					pvo.selected = 1;
				}
			}else if(pvo.type == "organization"){				
				ro.destination = "OrganizationImpl";			
				ro.addEventListener(FaultEvent.FAULT,flt);
				ro.addEventListener(ResultEvent.RESULT,updateRst);
				if(this.selected){//之前选中的，后面要删除
					ro.deleteHisCode(pvo);
					(this.data as HisOrganizationVo).selected = 0;
					pvo.selected = 0;
				}else{
					ro.addHisCode(pvo);
					(this.data as HisOrganizationVo).selected = 1;
					pvo.selected = 1;
				}
			}			
		}
		private function updateRst(evt:ResultEvent):void{
			Alert.show(ResourceManager.getInstance().getString("lang", "update_success"));
		}
		private function flt(evt:FaultEvent):void{
			Alert.show(evt.fault.getStackTrace());
		}
		private function showHandler(event:FlexEvent):void{
			pvo = this.data as HisOrganizationVo;
			
			if( pvo.selected == 1){
				this.selected = true;
			}else{
				this.selected = false;
			}
		}
	}
}